var base = getApp();
// var common = require('../../utils/preview.js');

// 加
Number.prototype.add = function (arg) {
  var r1, r2, m;
  try { r1 = this.toString().split(".")[1].length; } catch (e) { r1 = 0; }
  try { r2 = arg.toString().split(".")[1].length; } catch (e) { r2 = 0; }
  m = Math.pow(10, Math.max(r1, r2));
  return (this * m + arg * m) / m;
}
// 减
Number.prototype.subduct = function (arg) {
  var r1, r2, m, n;
  try { r1 = this.toString().split(".")[1].length; } catch (e) { r1 = 0; }
  try { r2 = arg.toString().split(".")[1].length; } catch (e) { r2 = 0; }
  m = Math.pow(10, Math.max(r1, r2));
  // n=(r1>=r2)?r1:r2; // 控制精度
  // return ((arg1*m-arg*m)/m).toFixed(n);
  return (this * m - arg * m) / m;
}
// 乘
Number.prototype.mul = function (arg) {
  var m = 0, s1 = this.toString(), s2 = arg.toString();
  try { m += s1.split(".")[1].length; } catch (e) { }
  try { m += s2.split(".")[1].length; } catch (e) { }
  return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
// 除
Number.prototype.div = function (arg) {
  var t1, t2, r1, r2;
  try { t1 = this.toString().split(".")[1].length; } catch (e) { t1 = 0; }
  try { t2 = arg.toString().split(".")[1].length; } catch (e) { t2 = 0; }
  r1 = Number(this.toString().replace(".", ""));
  r2 = Number(arg.toString().replace(".", ""));
  return (r1 / r2) * Math.pow(10, t2 - t1);
}

Page({
  data: {
    goodsList: [],
    totalPrice: 0,
    totalNum:0,
    selectAddress: {},
    selectedId: 0,
    url: base.path.url,
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({
      title: '确认订单'
    });
    //判断上次输入地址
    wx.request({
      url: base.path.api + 'Order.GetAddress',
      data: {
        md5key: base.user.session_key,
        aid: base.user.aid,
      },
      async: false,
      method: "POST",
      header: base.user.header,
      success: function (response) {
        var result = response.data;
        if (result.ret == 200) {
          that.setData({
            selectAddress: {
              name: result.data.receiver,
              phone: result.data.mobile,
              addr: result.data.address
            },
          });
        } else {
          
        }
      }
    });

    if (e.type == 'cart') {

      try {
        var value = wx.getStorageSync('cart_info');
        if (value) {
          var _total = 0;
          for (var k in value) {
            _total = parseInt(Number(_total).add(Number(value[k].sell_money).mul(parseInt(value[k].goods_num))) * 100) / 100;
          }
          that.setData({
            goodsList: value,
            totalPrice: base.returnFloat(_total)
          });
          return;
        }
      } catch (e) {
        wx.showToast({
          title: '参数错误',
          icon: 'loading',
          duration: '1000',
          success: function () {
            wx.navigateBack({
              url: '../cart/cart'
            })
          }
        });
      }
    } else {
      try {
        var value = wx.getStorageSync('buy_info');
        if (value) {
          var _total = 0;
          var totalNum=0;
          for (var k in value) {
            // _total += parseFloat(value[k].sell_money) * parseInt(value[k].goods_num);
            _total = parseInt(Number(_total).add(Number(value[k].sell_money).mul(parseInt(value[k].goods_num))) * 100) / 100;
            totalNum += parseInt(value[k].goods_num);
          }
          that.setData({
            goodsList: value,
            totalPrice: base.returnFloat(_total),
            totalNum: totalNum
          });
          return;
        }
      } catch (e) {
        wx.showToast({
          title: '参数错误',
          icon: 'loading',
          duration: '1000',
          success: function () {
            wx.navigateBack({
              url: '../cart/cart'
            })
          }
        });
      }
    }
  },

  
  onShareAppMessage: function () {
    return {
      title: base.user.shareTitle,
      desc: base.user.shareDesc,
      path: '/pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  // onShow: function (e) {
  //   this.onLoad(e);
  // },
  myaddrChange: function () {//触摸选择地址
    var _this = this;
    wx.chooseAddress({
      success: function (res) {
        // var content = res.userName + "," + res.postalCode + "," + res.provinceName + "," + res.cityName + "," + res.countyName + "," + res.detailInfo + "," + res.nationalCode + "," + res.telNumber
        _this.setData({
          selectAddress: {
            name: res.userName,
            phone: res.telNumber,
            addr: res.provinceName + ' ' + res.cityName + ' ' + res.countyName + ' ' + res.detailInfo
          },
        });
      }
    })
  },
 
  /**
   * 提交订单
   */
  submit: function () {
    var that = this;
    var numStr = "";
    var skuidStr="";
    var realname=that.data.selectAddress.name;
    var mobile=that.data.selectAddress.phone;
    var address= that.data.selectAddress.addr;
    var goodsList = that.data.goodsList;
    var detail="";
    if (goodsList.length != 0) {
      for (var k in goodsList) {
        numStr = numStr + goodsList[k].goods_num + ",";
        skuidStr = skuidStr + goodsList[k].skuid+",";
        detail = goodsList[k].name + "," + goodsList[k].skuname+","
      } 
      numStr = numStr.substring(0, numStr.length - 1);  
      skuidStr = skuidStr.substring(0, skuidStr.length - 1);  
      detail = detail.substring(0, detail.length - 1);  
      if (realname == undefined || realname == "" || realname == null){
        wx.showModal({
          showCancel: false,
          title: '',
          content: "收货信息不能为空"
        });
        return;
      }
    }
    wx.request({
      url: base.path.api + 'Order.CreateOrder',
      data: {
        md5key: base.user.session_key,
        aid: base.user.aid,
        quantity: numStr,
        skuid: skuidStr,
        realname: that.data.selectAddress.name,
        mobile: that.data.selectAddress.phone,
        address: that.data.selectAddress.addr
      },
      async: false,
      method: "POST",
      header: base.user.header,
      success: function (response) {
        var result = response.data;
        if (result.ret == 200) {
          var orderno = result.data;
          base.getPay(orderno, detail);
        }else{
          wx.showModal({
            showCancel: false,
            title: '',
            content: '商品库存不足'
          });
        }
      }
    });

  },
  
  
})